package com.ytkj.ygAssist.server;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.ytkj.ygAssist.server.util.HttpGetUtil;
import com.ytkj.ygAssist.server.util.JsonUtil;
import com.ytkj.ygAssist.tools.CacheData;
import com.ytkj.ygAssist.tools.MyLog;

/**
 *	查询66网数据
 */
public class GetDataBy66w {
	
	/**
	 * 查询66网中奖信息
	 */
	public static ArrayList<String[]> getUserBuyListBy66w(String goodsID) {
		String url = "http://www.66w.com/charts/index/getData?type=yungou&gid="+goodsID;
		String refererUrl = "http://www.66w.com";
		String content = HttpGetUtil.getHttpDataAsUTF_8(url, refererUrl);
		
		JsonObject jsonObject66w = new Gson().fromJson(content, JsonObject.class);
		if("1".equals(jsonObject66w.get("Status").toString())){
			JsonArray jsonArray = jsonObject66w.getAsJsonArray("Lists");
			List<Map<String, String>> Lists = new Gson().fromJson(jsonArray, new TypeToken<List<Map<String, String>>>() {}.getType());
			ArrayList<String[]> stringList = new ArrayList<String[]>();
			for(Map<String, String> jsonElement:Lists){
				String[] strings = new String[8];
				strings[0] = goodsID;
				strings[1] = jsonElement.get("codePeriod");
				if (CacheData.getSelectCacheDate(goodsID, strings[1]) != null) {
					continue;
				}
				strings[2] = jsonElement.get("codeRNO");
				strings[3] = jsonElement.get("userName");
				strings[4] = jsonElement.get("buyNum");
				strings[5] = jsonElement.get("luckyPos");
				String luckyBuyIdPos = jsonElement.get("luckyBuyIdPos");
				strings[6] = luckyBuyIdPos+"~"+(Integer.valueOf(luckyBuyIdPos)+Integer.valueOf(strings[4]));
				strings[7] = jsonElement.get("codeID");
				if("登录66w.com下载新版".equals(strings[3])){
					strings[3] = "正在查询中，请稍等";
					CloseableHttpClient HttpClient = HttpClients.createDefault();
					int FIdx = 1;
					int count = 0;
					do {
						String userStr = GetGoodsInfo.getUserBuyListEnd(strings[7], FIdx, HttpClient);
						JsonObject jsonObject = JsonUtil.fromJsonClear(userStr, JsonObject.class);
						if("0".equals(jsonObject.get("code").toString())){
							count = jsonObject.get("count").getAsInt();
							if(count > 280){
								break;
							}
							JsonArray data = jsonObject.getAsJsonArray("data");
							MyLog.testLog("数据", data);
							List<Map<String, String>> dList = new Gson().fromJson(data, new TypeToken<List<Map<String, String>>>() {}.getType());
							for(Map<String, String> jsonElementMap:dList){
								String userWeb = jsonElementMap.get("userWeb");
								String userName = jsonElementMap.get("userName");
								if(jsonElement.get("userWeb").equals(userWeb)){
									MyLog.testLog("中奖者", userName);
									strings[3] = userName;
									FIdx = count;
									break;
								}
							}
							FIdx+=10;
						}
					} while (FIdx < count);
				}
				MyLog.testLog("从66网下载中奖信息", strings);
				stringList.add(strings);
				SelectAssistPublishs.uploadYungouPublishs(strings);
			}
			return stringList;
		}
		return new ArrayList<String[]>();
	}
	
	public static String[] shopCartNew(String goodsID) {//查询66网中奖信息
		String url = "http://www.66w.com/charts/index/getprogress?type=yungou&gid="+goodsID;
		String refererUrl = "http://www.66w.com";
		String content = HttpGetUtil.getHttpDataAsUTF_8(url, refererUrl);
		JsonObject jsonObject = JsonUtil.fromJson(content, JsonObject.class);
//		String[] text = new String[] { codeID, period, bar01, bar02, bar03 };
		String[] text = new String[] { null, jsonObject.get("period").toString(),jsonObject.get("existingTimes").getAsString(), ""+(jsonObject.get("remainTimes").getAsInt()+jsonObject.get("existingTimes").getAsInt()), jsonObject.get("remainTimes").toString() };
		return text;
	}
}
